/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.service.impl;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.workcell.workdata.im.component.redis.MicroServerRedisTemplate;
import com.workcell.workdata.im.entity.MicroServer;
import com.workcell.workdata.im.model.vo.MicroServerVO;
import com.workcell.workdata.im.repository.MicroServerMenuRepository;
import com.workcell.workdata.im.repository.MicroServerRepository;
import com.workcell.workdata.im.repository.specification.MicroServerSpecification;
import com.workcell.workdata.im.service.MicroServerService;
import com.workcell.workdata.im.util.PageX;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class MicroServerServiceImpl implements MicroServerService {

	@Resource
	private MicroServerRepository microServerRepository;

	@Resource
	private MicroServerMenuRepository microServerMenuRepository;

	@Resource
	private MicroServerRedisTemplate microServerRedisTemplate;

	private final LoadingCache<Long, String> nameCache;

	@Autowired
	public MicroServerServiceImpl(){
		nameCache = CacheBuilder.newBuilder()
				.maximumSize(100)
				.expireAfterAccess(10, TimeUnit.MINUTES)
				.build(CacheLoader.from(id -> microServerRepository.findName(id)));
	}

	@Override
	public String getName(long id) {
		return nameCache.getUnchecked(id);
	}

	@Override
	public MicroServerVO findOne(Long id) {

		MicroServer server = microServerRedisTemplate.get(id);

		if (server != null){
			return MicroServerVO.of(server);
		}

		MicroServer newServer = microServerRepository.findById(id).orElse(null);

		if (newServer != null) {
			microServerRedisTemplate.save(newServer);
		}

		return MicroServerVO.of(newServer);
	}

	@Override
	public String getWebhook(Long id) {
		return microServerRepository.getWebhook(id);
	}


	@Override
	public List<MicroServerVO> findList(Long uid) {

		List<MicroServerVO> voList = PageX.map(MicroServerVO::of,microServerRepository.queryListByUid(uid));

		voList.forEach(server -> server.setMenuList(microServerMenuRepository.getList(server.getId())));

		return voList;
	}

	@Override
	public MicroServerVO findOne(String account) {
		return MicroServerVO.of(microServerRepository.findOne(account));
	}

	@Override
	public Page<MicroServerVO> search(long index , String name, Pageable pageable) {
		Specification<MicroServer> condition = MicroServerSpecification.search(index,name);
		Page<MicroServer> page =  microServerRepository.findAll(condition,pageable);

		return PageX.map(MicroServerVO::of,page);
	}

}
